import { circlePost } from '../server/circlePost.js';
import { formatTimeToText } from '../util/tool.js';

import { getUserIcon, getCirclePostImgUrl } from './file.js';

// 获取推荐帖子
export async function getRecommendPostList(self, lastId) {
	if (!lastId) {
		self.dyList = [];
	}
	try {
		let res = await circlePost.getRecommendPostList(lastId);
		if (res.success) {
			for (let i = 0; i < res.data.length; i++) {
				let item = res.data[i];
				// {
				// 	id: 1,
				// 	userIcon: '',
				// 	username: '小明',
				// 	title: '考研互助小组',
				// 	content: '有没有准备考研的同学，一起组队互相监督呀~ 我准备考浙大计算机，最近在刷数学，找个一起学习的伙伴。每天打卡，互相分享学习进度和心得，一起进步！如果你也在准备考研，或者有相关经验，欢迎加入我们~',
                //  imgList: 'xxx',      -- 由 imgIdList 字段计算而来
				// 	circlePostType: '学习',
				//  circlePostTypeTextColor: '',
				//  circlePostTypeBgColor: '',
				// 	commentCount: 5,
				// 	likeCount: 12,
				// 	time: '10分钟前',     -- 由 publishTime 字段计算而来
				//  isLike: false
				// }
				let imgIdList = JSON.parse(item.imgIdList);
				let imgList = [];
				for (let j = 0; j < imgIdList.length; j++) {
					let imgId = imgIdList[j];
					imgList.push({ id: imgId, url: await getCirclePostImgUrl(imgId)});
				}
				let post = {
					id: item.id,
					userIcon: await getUserIcon(item.userIcon),
					username: item.username,
					title: item.title,
					content: item.content,
                    imgList: imgList,
					circlePostType: item.circlePostType,
					circlePostTypeTextColor: item.circlePostTypeTextColor,
					circlePostTypeBgColor: item.circlePostTypeBgColor,
					commentCount: item.commentCount,
					likeCount: item.likeCount,
					time: formatTimeToText(item.publishTime),
					isLike: item.isLike
				};
				self.dyList.push(post);
			}
		}
	} catch (err) {
		console.log(err);
	}
}

// 